package cn.frh.order.service.impl;


import cn.frh.basic.service.impl.BaseServiceImpl;
import cn.frh.basic.util.CodeGenerateUtils;
import cn.frh.basic.util.PageList;
import cn.frh.order.domain.AdoptOrder;
import cn.frh.order.domain.OrderAddress;
import cn.frh.order.mapper.AdoptOrderMapper;
import cn.frh.order.mapper.OrderAddressMapper;
import cn.frh.order.query.AdoptOrderQuery;
import cn.frh.order.service.IAdoptOrderService;
import cn.frh.org.domain.Employee;
import cn.frh.org.mapper.EmployeeMapper;
import cn.frh.pay.constants.AliPayContansts;
import cn.frh.pay.domain.PayBill;
import cn.frh.pay.mapper.PayBillMapper;
import cn.frh.pay.service.impl.PayBillServiceImpl;
import cn.frh.pet.domain.Pet;
import cn.frh.pet.mapper.PetMapper;
import cn.frh.user.domain.Logininfo;
import cn.frh.user.domain.User;
import cn.frh.user.domain.UserAddress;
import cn.frh.user.mapper.UserAddressMapper;
import cn.frh.user.mapper.UserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

@Service
public class AdoptOrderServiceImpl extends BaseServiceImpl<AdoptOrder> implements IAdoptOrderService {

    @Autowired
    private PetMapper petMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private AdoptOrderMapper adoptOrderMapper;
    @Autowired
    private UserAddressMapper addressMapper;
    @Autowired
    private OrderAddressMapper orderAddressMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private PayBillMapper payBillMapper;
    @Autowired
    private PayBillServiceImpl payBillService;

    @Override
    public String submit(Map<String, Object> params, Logininfo currentLogininfo) {

        Long serviceMethod = Long.valueOf(params.get("service_method").toString()) ;
        Long addressId = Long.valueOf(params.get("address_id").toString()) ;
        Long paymethod = Long.valueOf(params.get("pay_method").toString()) ;//0 支付宝， 1微信 2 银联
        Long petId = Long.valueOf(params.get("pet_id").toString()) ;
        //宠物下架 下架时间
        Pet pet = petMapper.loadById(petId);
        //1.下架宠物
        //设置状态
        pet.setState(0);
        //设置下架时间
        pet.setOffsaletime(new Date());
        //2.绑定宠物领养人
        User user = userMapper.loadByLogininfoId(currentLogininfo.getId());
        pet.setUser_id(user.getId());
        petMapper.update(pet);
        //生成领养订单
          //保存订单
          //保存地址
        AdoptOrder order = initAdoptOrder(pet, user);
        //由于后面生成支付单 需要统一支付标识 所以提前生成
        String paySn = CodeGenerateUtils.generateUnionPaySn();
        order.setPaySn(paySn);
        adoptOrderMapper.save(order);

        UserAddress address = addressMapper.loadById(addressId);
        OrderAddress orderAddress = initOrderAddress(order, address);
        orderAddressMapper.save(orderAddress);

        //生成支付单并支付
        PayBill bill = initPayBill(paymethod, user, order);
        payBillMapper.save(bill);
        //@TODO 支付超时定时器
        //封装统一支付接口
        return payBillService.pay(bill);
    }

    private PayBill initPayBill(Long paymethod, User user, AdoptOrder order) {
        PayBill bill = new PayBill();
        bill.setDigest("[摘要]对"+order.getOrderSn()+"支付订单！");
        bill.setMoney(order.getPrice());
        bill.setUnionPaySn(order.getPaySn());
        bill.setLastPayTime(order.getLastPayTime());//最后支付时间
        bill.setPayChannel(paymethod);//0 余额 1 支付宝 2 微信 3 银联
        bill.setBusinessType(AliPayContansts.ADOPT_ORDER);
        bill.setBusinessKey(order.getId());
        bill.setUser_id(order.getUser_id());
        bill.setShop_id(order.getShop_id());
        bill.setNickName(user.getUsername());//反第三范式 冗余
        return bill;
    }

    private OrderAddress initOrderAddress(AdoptOrder order, UserAddress address) {
        OrderAddress orderAddress = new OrderAddress();
        BeanUtils.copyProperties(address,orderAddress);
        orderAddress.setOrder_id(order.getId());
        orderAddress.setOrderSn(order.getOrderSn());
        return orderAddress;
    }

    private AdoptOrder initAdoptOrder(Pet pet, User user) {
        AdoptOrder order = new AdoptOrder();
        order.setDigest("[摘要]对"+pet.getName()+"领养订单！");
        order.setState(0);//待支付
        order.setPrice(pet.getSaleprice());//售价
        //创建订单唯一标识
        String orderSn = CodeGenerateUtils.generateOrderSn(user.getId());
        order.setOrderSn(orderSn);
        order.setPet_id(pet.getId());
        order.setUser_id(user.getId());
        order.setShop_id(pet.getShop_id());
        //最后支付时间
        order.setLastPayTime(new Date(System.currentTimeMillis() + 15*60*1000));
        return order;
    }


    @Override
    public PageList<AdoptOrder> admin(AdoptOrderQuery query, Logininfo currentLogininfo) {
        Employee employee = employeeMapper.loadByLogininfoId(currentLogininfo.getId());
        if (employee.getShop_id()!=null){
            query.setShopId(employee.getShop_id());
        }
        return super.queryPage(query);
    }

    @Override
    public PageList<AdoptOrder> user(AdoptOrderQuery query, Logininfo currentLogininfo) {
        User user = userMapper.loadByLogininfoId(currentLogininfo.getId());
            query.setUserId(user.getId());
        return super.queryPage(query);
    }

}
